#!/usr/bin/python
#coding=utf-8
import time
import sys
interface_dict={}
f = open('/proc/net/dev', 'r')
for line in f.readlines()[3:]:
    INTERFACE=line.split(":")[0].lstrip()
    STATS = []
    def rx():
        ifstat = open('/proc/net/dev').readlines()
        for interface in  ifstat:
            if INTERFACE in interface:
                stat = float(interface.split(":")[1].split()[0])
                STATS[0:] = [stat]
    def tx():
        ifstat = open('/proc/net/dev').readlines()
        for interface in  ifstat:
            if INTERFACE in interface:
                stat = float(interface.split()[9])
                STATS[1:] = [stat]
    try:
        rx()
        tx()
        for i in range(1):
            time.sleep(1)
            rxstat_o = list(STATS)
            rx()
            tx()
            RX = float(STATS[0])
            RX_O = rxstat_o[0]
            TX = float(STATS[1])
            TX_O = rxstat_o[1]
            #In流量
            #RX_RATE = round((RX - RX_O)/1024,3)
            RX_RATE = round((RX - RX_O),3)
            #Out流量
            TX_RATE = round((TX - TX_O),3)
            interface_dict["netflow_" + INTERFACE] = str(RX_RATE) + "," + str(TX_RATE)
    except KeyboardInterrupt, e:
        print "\npyifstat exited"

